package br.pucrio.inf.tamdera.utils;

import java.util.ArrayList;
import java.util.List;

import br.pucrio.inf.tamdera.prolog.utils.PrologIntegrationConstants;

public class ConverterPrologTamderaUtils {

	public static String getConvertedNameOfPrologClass ( String prologClass )
	{
		int lastIndexOfPath = prologClass.lastIndexOf( PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_PROLOG );

		String classNameStr = prologClass.substring( lastIndexOfPath + 1, prologClass.length() );
		classNameStr = classNameStr.substring(1, classNameStr.length() - 1);
		String packageStr =  prologClass.substring( 0, lastIndexOfPath );
		prologClass = packageStr + PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_TAMDERA_DSL + classNameStr;

		prologClass = prologClass.replaceAll( PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_PROLOG,
				PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_TAMDERA_TO_REPLACE );

		prologClass = prologClass.replaceAll( "\\'",
				"" );

		return prologClass;
	}

	public static List<String> getConvertedNamesOfPrologClasses ( List<String> prologClasses )
	{
		List<String> newList = new ArrayList<String>();

		for ( String str : prologClasses )
		{
			String convertedClassName = getConvertedNameOfPrologClass( str );
			newList.add( convertedClassName );
		}
		return newList;
	}

	public static Pair<String, String> getPrologClassNameFromTamderaClassName( String classNameWithPackage ) 
	{

		Pair<String, String> result = new Pair<String, String>();

		int lastIndexOfPath = classNameWithPackage.lastIndexOf( PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_TAMDERA_DSL );

		String packageStr = classNameWithPackage.substring( 0, lastIndexOfPath );

		packageStr = packageStr.replaceAll( PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_TAMDERA_TO_REPLACE,
				PrologIntegrationConstants.PACKAGE_SEPARATOR_IN_PROLOG );

		String className = classNameWithPackage.substring( lastIndexOfPath + 1, classNameWithPackage.length() );

		result.setFirst( packageStr );
		result.setSecond( className );
		
		

		return result;
	}		
}
